home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-2.iso / os2 / rsynth1.zip / mkdictdb.c < prev    next >
C/C++ Source or Header  |  1994-11-01  |  3KB  |  135 lines

  1. #include <config.h>
  2. #include "proto.h"
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <useconfig.h>
  6. #include <gdbm.h>
  7. #include "trie.h"
  8. #include "darray.h"
  9. #include "phones.h"
  10.  
  11. /* Don't force block size let gdbm/os decide */
  12. #define BLOCK_SIZE 0
  13.  
  14. #ifndef GDBM_FAST
  15. /* Tolerate older versions of gdbm ... */
  16. #define GDBM_FAST 0
  17. #endif
  18.  
  19. trie_ptr phones;
  20.  
  21. static void enter_phones PROTO((void));
  22. static void 
  23. enter_phones()
  24. {
  25.  int i;
  26.  char *s;
  27.  for (i = 1; (s = ph_name[i]); i++)
  28.   trie_insert(&phones, s, (void *) i);
  29. }
  30.  
  31. static void enter_words PROTO((GDBM_FILE db, FILE * f));
  32. static void
  33. enter_words(db, f)
  34. GDBM_FILE db;
  35. FILE *f;
  36. {
  37.  char buf[4096];
  38.  while (fgets(buf, sizeof(buf), f))
  39.   {
  40.    char *s = buf;
  41.    char *h = strchr(s, '#');
  42.    if (h)
  43.     *h = '\0';
  44.    while (isspace(*s))
  45.     s++;
  46.    if (*s)
  47.     {
  48.      char *p = s;
  49.      while (isalpha(*p) || *p == '-' || *p == '\'')
  50.       {
  51.        if (islower(*p))
  52.         *p = toupper(*p);
  53.        p++;
  54.       }
  55.      if (isspace(*p))
  56.       {
  57.        char codes[4096];
  58.        char *d = codes;
  59.        int ok = 1;
  60.        datum key;
  61.        key.dptr = s;
  62.        key.dsize = p - s;
  63.        while (*p && ok)
  64.         {
  65.          unsigned code;
  66.          while (isspace(*p))
  67.           p++;
  68.          if (*p)
  69.           {
  70.            char *e = p;
  71.            while (isalpha(*e) || *e == '1' || *e == '2')
  72.             {
  73.              if (islower(*e))
  74.               *e = toupper(*e);
  75.              e++;
  76.             }
  77.            if (*e == '0')
  78.             *e++ = ' ';
  79.            if (e > p && (code = (unsigned) trie_lookup(&phones, &p)))
  80.             *d++ = code;
  81.            else
  82.             {
  83.              fprintf(stderr, "Bad code %.*s>%s", (int)(p - s), s, p);
  84.              ok = 0;
  85.              break;
  86.             }
  87.           }
  88.         }
  89.        if (ok)
  90.         {
  91.          datum data;
  92.          data.dptr = codes;
  93.          data.dsize = d - codes;
  94.          gdbm_store(db, key, data, GDBM_INSERT);
  95.         }
  96.       }
  97.      else
  98.       {
  99.        if (*p != '(')
  100.         fprintf(stderr, "Ignore (%c) %s", *p, s);
  101.       }
  102.     }
  103.   }
  104. }
  105.  
  106. int main PROTO((int argc, char *argv[], char *env[]));
  107.  
  108. int
  109. main(argc, argv, envp)
  110. int argc;
  111. char *argv[];
  112. char *envp[];
  113. {
  114.  if (argc == 3)
  115.   {
  116.    FILE *f = fopen(argv[1], "r");
  117.    if (f)
  118.     {
  119.      GDBM_FILE db = gdbm_open(argv[2], BLOCK_SIZE, GDBM_WRCREAT | GDBM_FAST, 0644, NULL);
  120.      if (db)
  121.       {
  122.        enter_phones();
  123.        enter_words(db, f);
  124.        gdbm_close(db);
  125.       }
  126.      else
  127.       perror(argv[2]);
  128.      fclose(f);
  129.     }
  130.    else
  131.     perror(argv[1]);
  132.   }
  133.  return 0;
  134. }
  135.